package com.ruoyi.classai.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.classai.domain.PxTraining;
import com.ruoyi.classai.domain.PxTrainingCourse;
import com.ruoyi.classai.service.IPxTrainingCourseService;
import com.ruoyi.classai.service.IPxTrainingService;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;


/**
 * 培训信息Controller
 * 
 * @author lcj
 * @date 2025-06-17
 */
@Tag(name = "培训管理接口")
@RestController
@RequestMapping("/training")
public class PxTrainingController extends BaseController
{
    @Autowired
    private IPxTrainingService pxTrainingService;
    @Autowired
    private IPxTrainingCourseService trainingCourseService;

    /**
     * 查询培训信息列表
     * @param trainingTitle 培训名称
     * @param trainingType 培训类型
     * @param trainingStatus 培训状态(0未开始1已开始2已结束)
     * @return 培训信息
     */
    @Operation(summary = "查询培训信息列表")
    @RequiresPermissions("classai:training:list")
    @GetMapping("/list")
    public TableDataInfo list(String trainingTitle, String trainingType,Integer trainingStatus) {
        startPage();
        List<PxTraining> list = pxTrainingService.list(Wrappers.<PxTraining>lambdaQuery()
                        .eq(PxTraining::getStatus,Constants.COMMON_SUCCESS)
                .like(StringUtils.isNotEmpty(trainingTitle),PxTraining::getTrainingTitle,trainingTitle)
                .eq(StringUtils.isNotEmpty(trainingType),PxTraining::getTrainingType,trainingType)
                .eq(StringUtils.isNotEmpty(SecurityUtils.getLoginUser().getSysUser().getSchoolId()),PxTraining::getSchoolId,SecurityUtils.getLoginUser().getSysUser().getSchoolId())
                .le(Objects.nonNull(trainingStatus)&&trainingStatus==1,PxTraining::getStartTime, DateUtil.date()) //开始时间小于等于当前时间
                .ge(Objects.nonNull(trainingStatus)&&trainingStatus==1,PxTraining::getEndTime,DateUtil.date()) //结束时间大于等于当前时间
                .le(Objects.nonNull(trainingStatus)&&trainingStatus==2,PxTraining::getEndTime,DateUtil.date()) //结束时间小于等于当前时间
                .ge(Objects.nonNull(trainingStatus)&&trainingStatus==0,PxTraining::getStartTime,DateUtil.date())); //开始时间大于等于当前时间
        return getDataTable(list);
    }

    /**
     * 导出培训信息列表
     */
    @RequiresPermissions("classai:training:export")
    @Log(title = "培训信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PxTraining pxTraining)
    {
        List<PxTraining> list = pxTrainingService.list(Wrappers.query(pxTraining));
        ExcelUtil<PxTraining> util = new ExcelUtil<PxTraining>(PxTraining.class);
        util.exportExcel(response, list, "培训信息数据");
    }

    /**
     * 获取培训信息详细信息
     */
    @Operation(summary = "根据ID查询培训信息")
    @RequiresPermissions("classai:training:query")
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(pxTrainingService.getById(id));
    }

    /**
     * 新增培训信息
     */
    @Operation(summary = "新增培训信息")
    @RequiresPermissions("classai:training:add")
    @Log(title = "培训信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody PxTraining pxTraining) {
        //学校id
        String schoolId= SecurityUtils.getLoginUser().getSysUser().getSchoolId();
        pxTraining.setSchoolId(schoolId);
        return toAjax(pxTrainingService.save(pxTraining));
    }

    /**
     * 修改培训信息
     */
    @Operation(summary = "修改培训信息")
    @RequiresPermissions("classai:training:edit")
    @Log(title = "培训信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody PxTraining pxTraining) {
        //学校id
        String schoolId= SecurityUtils.getLoginUser().getSysUser().getSchoolId();
        pxTraining.setSchoolId(schoolId);
        return toAjax(pxTrainingService.updateById(pxTraining));
    }

    /**
     * 删除培训信息
     */
    @Operation(summary = "删除培训信息")
    @RequiresPermissions("classai:training:remove")
    @Log(title = "培训信息", businessType = BusinessType.DELETE)
	@GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        for (String id : ids) {
            //查询课程信息
            long count = trainingCourseService.count(Wrappers.<PxTrainingCourse>lambdaQuery().eq(PxTrainingCourse::getTrainingId, id)
                    .eq(PxTrainingCourse::getStatus, Constants.COMMON_SUCCESS));
            if (count > 0) {
                return AjaxResult.success("培训计划下有课程，删除失败");
            }
            PxTraining training = new PxTraining();
            training.setId(id);
            training.setStatus(Constants.COMMON_DELETE);
            pxTrainingService.updateById(training);
        }
        return AjaxResult.success("删除成功");
    }



}
